package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.monitoring.j_monitoring.entity.MDailyInspection;
import org.jeecg.modules.monitoring.j_monitoring.entity.MDailyInspectionFile;
import org.jeecg.modules.monitoring.j_monitoring.entity.MDailyInspectionDTO;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MDailyInspectionFileMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MDailyInspectionMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMDailyInspectionFileService;
import org.jeecg.modules.monitoring.j_monitoring.service.IMDailyInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @Description: m_daily_inspection
 * @Author: xiaoyq
 * @Date:   2023-10-10
 * @Version: V1.0
 */
@Service
public class MDailyInspectionServiceImpl extends ServiceImpl<MDailyInspectionMapper, MDailyInspection> implements IMDailyInspectionService {


    @Resource
    private MDailyInspectionFileMapper fileMapper;


    @Autowired
    private ItScreenService screenService;

    public List<MDailyInspection> getListByTime(String start,String end){
        return baseMapper.getListByTime(start,end);
    }

    @Override
    public List<MDailyInspectionFile> getImageList(String parentId) {
        return fileMapper.selectByParentId(parentId);
    }

    public List<MDailyInspectionDTO> getTodayInspection(String userId){
        List<MDailyInspectionDTO> list =  baseMapper.getTodayInspection(userId);


        for (MDailyInspectionDTO dailyInspection : list){
            dailyInspection.setUploadFileList(fileMapper.selectByParentId(dailyInspection.getId()));
        }


        return list;
    }

    public List<MDailyInspectionFile> getFileListById(String id){
        return fileMapper.selectByParentId(id);
    }

    public List<MDailyInspectionDTO> getPlanInspection(String userId){
        List<MDailyInspectionDTO> list =  baseMapper.getPlanInspection(userId);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    public int saveTodayInspection(MDailyInspectionDTO[] dataList){
        Calendar cal= Calendar.getInstance();
        String time = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE);
        for (MDailyInspectionDTO dailyInspection : dataList){
            fileMapper.deleteByParentId(dailyInspection.getId());

            if (dailyInspection.getUploadFileList().size() >0){
                dailyInspection.setCompletionStatus("已上传");
            }

            for (MDailyInspectionFile file : dailyInspection.getUploadFileList()){
                file.setParentId(dailyInspection.getId());
                fileMapper.insert(file);
            }
            try {
                Map<String ,Object> map =  screenService.getRoomTemHum(dailyInspection.getMachineRoomName());
                if (map.containsKey("tem")){
                    dailyInspection.setTemperature(map.get("tem").toString());
                }
                if (map.containsKey("hum")){
                    dailyInspection.setHumidity(map.get("hum").toString());
                }
            }catch (Exception e){
                e.printStackTrace();
            }

            dailyInspection.setRecordTime(time);
            this.saveOrUpdate(dailyInspection);
        }

        return 1;
    }
}
